#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#include<netinet/in.h>

#define BUF_SIZE 100

int main(void){

    int server_sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

    struct sockaddr_in server_addr;
    memset(&server_addr, 0, sizeof(server_addr));
    server_addr.sin_family = AF_INET;
    server_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
    server_addr.sin_port = htons(1234);
    bind(server_sock, (struct sockaddr*)&server_addr, sizeof(server_addr));
    listen(server_sock, 20);

    struct sockaddr_in client_addr;
    socklen_t client_addr_size = sizeof(client_addr);
    char buffer[BUF_SIZE] = {0};

    while(1){
        int client_sock = accept(server_sock, (struct sockaddr*)&client_addr, &client_addr_size);

        // send message to client
        // char str[] = "Hello, world!";
        // write(client_sock, str, sizeof(str));

        // receive client message and return it
        int strlen_ = recv(client_sock, buffer, BUF_SIZE, 0);
        send(client_sock, buffer, strlen_, 0);

        close(client_sock);
        memset(buffer, 0, BUF_SIZE);}

    close(server_sock);

    return 0;
}